package cn.iocoder.yudao.module.picturebook.service.comment;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.picturebook.dal.mysql.user.PBUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.comment.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.comment.CommentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.picturebook.dal.mysql.comment.CommentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.picturebook.enums.ErrorCodeConstants.*;

/**
 * 帖子评论 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private PBUserMapper pBUserMapper;

    @Override
    public Long createComment(CommentSaveReqVO createReqVO, String userId) {
        // 插入
        CommentDO comment = BeanUtils.toBean(createReqVO, CommentDO.class);
        comment.setCreator(userId);
        commentMapper.insert(comment);

        // 返回
        return comment.getCommentId();
    }

    @Override
    public void updateComment(CommentSaveReqVO updateReqVO) {
        // 校验存在
        validateCommentExists(updateReqVO.getCommentId());
        // 更新
        CommentDO updateObj = BeanUtils.toBean(updateReqVO, CommentDO.class);
        commentMapper.updateById(updateObj);
    }

    @Override
    public void deleteComment(Long id) {
        // 校验存在
        validateCommentExists(id);
        // 删除
        commentMapper.deleteById(id);
    }

    @Override
    public void deleteCommentListByIds(List<Long> ids) {
        // 删除
        commentMapper.deleteByIds(ids);
    }


    private void validateCommentExists(Long id) {
        if (commentMapper.selectById(id) == null) {
            throw exception(COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public CommentDO getComment(Long id) {
        return commentMapper.selectById(id);
    }

    @Override
    public PageResult<CommentRespVO> getCommentPage(CommentPageReqVO pageReqVO) {
        PageResult<CommentDO> commentDOPageResult = commentMapper.selectPage(pageReqVO);
        PageResult<CommentRespVO> commentRespVOPageResult = BeanUtils.toBean(commentDOPageResult, CommentRespVO.class);
        for (CommentRespVO commentRespVO : commentRespVOPageResult.getList()) {
            commentRespVO.setAvatar(pBUserMapper.selectById(commentRespVO.getCreator()).getAvatar());
            commentRespVO.setCreator(pBUserMapper.selectById(commentRespVO.getCreator()).getRealName());
        }
        return commentRespVOPageResult;
    }

}